<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/6/27 0027
// +----------------------------------------------------------------------
// | Description:  
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\validate\CheckAuth;
use app\common\controller\AdminBaseController;
use \app\admin\model\AuthRule as AuthRules;
use Jrk\Tree;
use think\Exception;
use think\facade\Db;
use think\Request;

class AuthRule extends AdminBaseController
{


    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        $this->model = new AuthRules();
    }


    /**
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/6/28 0028
     * @describe: 编辑添加
     */
    public function addAuth()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            //验证提交的数据
            $validate = new CheckAuth();
            if (!$validate->check($data)) {
                return parent::JsonReturn($validate->getError(), 0);
            }
            if (!isset($data['id'])) {
                $res = AuthRules::where(['title' => "" . $data['title'] . ""])->find();
                if ($res) {
                    return parent::JsonReturn($data['title'] . "该名称已存在", 0);
                }
            }
            return $this->model->doAll($data);
        }
        //获取菜单列表
        $tree = Tree::toFormatTree(AuthRules::menuList());
        $pid = $this->request->param("id/d");//父id
        $info = AuthRules::find($pid);

        $this->assign(compact("tree", "info", "pid"));

        return $this->fetch("auth_rule");
    }


    /**
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/6/28 0028
     * @describe:新增子菜单
     */
    public function addSon()
    {
        //获取菜单列表
        $tree = Tree::toFormatTree(AuthRules::menuList());
        $pid = $this->request->param("id/d");//父id
        $info = [];
        $this->assign("pid", $pid);
        $this->assign(compact("tree", "info"));
        return $this->fetch("auth_rule");
    }


    /**
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/7/2 0002
     * @describe:菜单添加节点
     */
    public function addNode()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            if (empty($data['title']) || empty($data['name'])) {
                return parent::JsonReturn("请填写节点名称地址", 0);
            }
            $num = AuthRules::where("pid", $data['pid'])->where("type", 2)->count("id");
            $all = count($data['title']) + $num;
            if ($all > 8) {
                return parent::JsonReturn("菜单节点总共超过8，已存在{$num}个节点", 0);
            }
            try {
                $arr = [];
                foreach ($data['title'] as $k => $v) {
                    $arr[] = [
                        'pid' => $data['pid'],
                        'type' => 2,
                        'auth_open' => $data['auth_open'],
                        'status' => $data['status'],
                        'title' => $v,
                        'name' => $data['name'][$k],
                        'create_time' => time()
                    ];
                }
                $res = Db::name("auth_rule")->insertAll($arr);
                if ($res) {
                    return parent::JsonReturn("添加成功");
                }
                return parent::JsonReturn("添加失败", 0);
            } catch (Exception $exception) {
                return parent::JsonReturn($exception->getMessage(), 0);
            }
        }
        $pid = $this->request->param("id/d");//id
        $info = AuthRules::find($pid);
        $this->assign(compact("info", "pid"));
        return $this->fetch();
    }

    /**
     * @return mixed
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @date: 2020/6/29 0029
     * @describe:删除选中的菜单
     */
    public function del_all()
    {
        if (IS_AJAX) {
            $ids = $this->request->post("ids");
            return $this->model->delAll($ids);
            //dd($ids);
        }
    }


}